﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using DataAccessLayer;
using BusinessLogicLayer;

namespace ChamCong
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        NhanVienBLL NVBLL = new NhanVienBLL();
        MenuBLL MBLL = new MenuBLL();
        PhanQuyenBLL pq = new PhanQuyenBLL();
        QuanLyPhanQuyenBLL q = new QuanLyPhanQuyenBLL();
        int _MaNV;
        protected void Page_Load(object sender, EventArgs e)
        {
            string strPath = this.Page.ToString().Substring(4, this.Page.ToString().Substring(4).Length - 5) + ".aspx";
            string strFullPath = strPath.Replace("_", "/");
            if (MBLL.LayThongTinMenuItemTheoDuongDan(strFullPath) != null)
            {
                lblTitlePage.Text = "<h1>" + MBLL.LayThongTinMenuItemTheoDuongDan(strFullPath).TenTrang + "</h1>";
                Page.Title += "Quản lý Chấm công - " + MBLL.LayThongTinMenuItemTheoDuongDan(strFullPath).TenTrang;
            }
            
            var returnUrl = Request.RawUrl;
            _MaNV = Convert.ToInt32(Session["MaNV"]);
            if (Session["MaNV"] == null)
            {
                LoginLink.Visible = true;

                if (returnUrl != null)
                {
                    //returnUrl = Request.QueryString["ReturnUrl"];
                    Response.Redirect("~/TaiKhoan/DangNhap.aspx?ReturnUrl=" + returnUrl); 
                }
                else
                {
                    Response.Redirect("~/TaiKhoan/DangNhap.aspx");
                }

                LoginLink.NavigateUrl = "~/TaiKhoan/DangNhap.aspx?ReturnUrl=" + returnUrl;
                Response.Write("<script>alert('Vui lòng đăng nhập!');window.location.href='/TaiKhoan/DangNhap.aspx';</script>");
            }
            else
            {
                if (!IsPostBack)
                {
                    Literal2.Text = LoadMenuByRole();
                    List<NHANVIEN> ThongTinNhanVien = new List<NHANVIEN>();
                    ThongTinNhanVien = NVBLL.TimKiem(Convert.ToInt32(Session["MaNV"]));
                    lblLogin.Visible = true;
                    lblUserName.Visible = true;
                    LogoutLink.Visible = true;
                    lblLogin.Text = "Xin chào:";
                    lblUserName.Text = ThongTinNhanVien[0].HoTen;
                }
            }
        }
        private string LoadMenuByRole()
        {
            string leftMenu = "";
            string htmlMenuByRole = "";

            List<MENU_TRAI> dtbRootByRole = MBLL.LayDSMenuCapMot();

            foreach (var item in dtbRootByRole)
            {
                htmlMenuByRole += "<h3><a href=\"#\">" + item.TenTrang + "</a></h3>";
                htmlMenuByRole += "<div>";
                List<MENU_TRAI> Cap2TheoRole = MBLL.LayDSMenuCapHaiTheoRole((int)item.CapMot, _MaNV);

                foreach (var item2 in Cap2TheoRole)
                {
                    htmlMenuByRole += @"<li style='padding-left:3px; line-height:20px'><a href='" + Page.ResolveUrl("~/" + item2.DuongDan) + "'>" + item2.TenTrang + "</a></li>";
                }
                htmlMenuByRole += "</div>";
                if (Cap2TheoRole.Count > 0)
                {
                    leftMenu += htmlMenuByRole;
                }
                htmlMenuByRole = "";
            }
            return leftMenu;
        }
        private string LoadMenu()
        {
            string htmlMenu = "";
            
            List<MENU_TRAI> dtbRoot = MBLL.LayDSMenuCapMot();

            foreach (var item in dtbRoot)
            {
                htmlMenu += "<h3><a href=\"#\">" + item.TenTrang + "</a></h3>";
                htmlMenu += "<div>";
                List<MENU_TRAI> Cap2 = MBLL.LayDSMenuCapHai((int)item.CapMot);
                foreach (var item2 in Cap2)
                {
                    htmlMenu += @"<li style='padding-left:3px; line-height:20px'><a href='" + Page.ResolveUrl("~/" + item2.DuongDan) + "'>" + item2.TenTrang + "</a></li>";
                }
                htmlMenu += "</div>";
            }
            return htmlMenu;
        }
    }
}
